// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var leftMenu = require("../leftMenu/leftMenu");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./pickupDetail.css");
	var css_consent = require("./pickupRansomConsent.css");
	var js_platform_main = require("./platform_main.js");
	var valuationCal = require("valuationCal");
	var tools = require("tools");
	var pickupId;
	var pageIndex = 1,
		pageSize = 5,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var html_oppose_pickup = require("./html_opposePickupApplication.html");
	var init = function(page) {
		pickupId = page.params.pickupId;
		//		pickupId = 51;	
		inquireRansomPickupApplicationDetail();
		tools.inquireCommonSelectorList(9, $(".delayReson"));
		$("#pincupRansomPickupId").val(pickupId);
		leftMenu.init(6);
		js_platform_main.init(0);
		$(".left").removeClass("none");
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#pickupRansomConsentBtn").unbind("click").click(function() {
			if($("#adjustRansomAmount1").val()==""){
				$("#adjustRansomAmount1").val(0);
			}
			var pickupConsentAdjustPrice = $(".pickupConsentAdjustPrice").val();
			if(pickupConsentAdjustPrice!=0 && pickupConsentAdjustPrice!=""){
				var delayReson =$(".delayReson").val();
			}
			databind.submitDom($("#pickupRansomConsent"), function() {
				base.alert(1, "同意赎货成功！");
				router.route("main/pickupRansomList.html");
			});
		});
		
		$("#pickupRansomConsent").on("change", ".pickupRansomConsentAdjustPrice", function(){
			var p = 0;
			$.each($("#pickupRansomConsent .pickupRansomConsentAdjustPrice"), function() {
				if($.trim($(this).val())==""){
					p += 0;
				}else{
					p += $.trim($(this).val())*1;
				}
			});
			$("#adjustRansomAmount").html(p);
			$("#adjustRansomAmount1").val(p);
			$("#shouldRansomReturn").html($("#pickupRansomConsentEstimateAmount").html()*1+$("#pickupRansomConsentBulkstockInterest").html()*1+$("#pickupRansomConsentBulkstockExpense").html()*1+$("#adjustRansomAmount").html()*1);
		});
	};

	//赎货申请明细
	var inquireRansomPickupApplicationDetail = function() {
		base.fnAjax({
			action: "inquireRansomPickupApplicationDetail.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var inventoryIds = [];
					var financeId = data.datas[0].financeId;
					var cutoffDate = data.datas[0].deliveryDate.substring(0, 10);
					var quantitys = [];
					var bulkstockIds = [];
					$.each(data.datas[0].containerInventorys, function() {
						if($.trim(this.inventoryId)!=""&&$.trim(this.inventoryId)!=null){
							inventoryIds.push(this.inventoryId);
						}
					});
					$.each(data.datas[0].pickupBulkstocks, function() {
							bulkstockIds.push(this.bulkstockId);
							quantitys.push(this.quantity);
					});
					
					if(inventoryIds.length == 0){
						interestEstimationFinanceBulkstock(cutoffDate, quantitys, bulkstockIds, financeId);
					}else{
						interestEstimationFinanceInventory(cutoffDate, inventoryIds, financeId);
					}
					
					juicer.register("getValueByKey", constant.getValueByKey);

					var tpl = $("#pickupRansomConsentBulkstockTpl").html();
					var html = juicer(tpl, data);
					$("#pickupRansomConsentBulkstockBody").html(html);
					
					var dp = 0;
					$.each(data.datas[0].pickupBulkstocks, function(i, value) {
						dp+=this.financePrice*1;	
					});
					$("#pickupRansomConsentEstimateAmount").html(dp);
					$("#pickupRansomConsentEstimateAmount1").val(dp);

					//重新估值
					$("#pickupRansomConsent .valuation_consent").unbind("click").click(function() {
						var unit ="";
						var quantity=0;
						if($(this).attr("measurement")==1){
							unit = "体积：";
							quantity = $(this).attr("volume");
						}else if($(this).attr("measurement")==2){
							unit = "重量：";
							quantity = $(this).attr("weight");
						}	
						var conversionfactor = $(this).attr("conversionfactor");
						valuationCal.showCal("重新估值", $(this).attr("bulkstockInfoId"),unit,quantity, $(this).parent().find(".pickupConsentValuation"),$(this).parents("tr").find(".bulkstockPrice"),conversionfactor);
					
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	//利息估算-指定货箱
	var interestEstimationFinanceInventory = function(cutoffDate, inventoryIds, financeId) {
		base.fnAjax({
			action: "interestEstimationFinanceInventory1.json",
			async: false,
			oData: {
				cutoffDate: cutoffDate,
				inventoryId: inventoryIds,
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$("#pickupRansomConsentBulkstockInterest").html(data.datas[0].interest);
					$("#pickupRansomConsentBulkstockInterest1").val(data.datas[0].interest);
					$("#pickupRansomConsentBulkstockExpense").val(data.datas[0].expense);
				} else {
					 base.alert(2,"信息获取失败");
				}
			}
		});
	};
	
	//利息估算-融资货物
	var interestEstimationFinanceBulkstock = function(cutoffDate, quantitys, bulkstockIds, financeId) {
		base.fnAjax({
			action: "interestEstimationFinanceBulkstock.json",
			async: false,
			oData: {
				cutoffDate: cutoffDate,
				quantity: quantitys,
				financeId: financeId,
				bulkstockId: bulkstockIds
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$("#pickupRansomConsentBulkstockInterest").html(data.datas[0].interest);
					$("#pickupRansomConsentBulkstockInterest1").val(data.datas[0].interest);
					$("#pickupRansomConsentBulkstockExpense").val(data.datas[0].expense);
				} else {
					 base.alert(2,"信息获取失败");
				}
			}
		});
	};


	//最新商品采集价格列表
	var inquireBulkstockLatestCollectionList = function(bulkstockInfoId) {
		base.fnAjax({
			action: "inquireBulkstockLatestCollectionList.json",
			async: false,
			oData: {
				pageSize: pageSize,
				pageIndex: pageIndex,
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockCollectionListTpl").html();
					var html = juicer(tpl, data);
					$("#bulkstockCollectionListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#valuationPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockLatestCollectionList();
						});

					$(".priceCheck").on("click", function() {
						$(".valuationRegionPrice").val(($(this).parents("tr").find(".price").html()).toFixed(6));
						var valuationRegionPrice = $(".valuationRegionPrice").val();
						var valuationQuantity = $(".valuationQuantity").val();
						if (valuationRegionPrice == "") {
							valuationRegionPrice = 0;
						}
						if (valuationQuantity == "") {
							valuationQuantity = 0;
						}
						$("#valuationPrice").html((valuationRegionPrice * valuationQuantity).toFixed(2));
					});
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});